package com.zhang.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.zhang.mapper.UserMapper;
import com.zhang.pojo.Nation;
import com.zhang.pojo.RespBean;
import com.zhang.pojo.Users;
import com.zhang.service.NationService;
import com.zhang.service.UserService;
import lombok.NonNull;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private NationService nationService;

    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportEmployee(HttpServletResponse response){
        //1.查询出全部的员工数据
        List<Users> users = userMapper.getAll();
        //2.
        ExportParams params = new ExportParams("员工表","员工表", ExcelType.HSSF);
        //3.
        Workbook workbook = ExcelExportUtil.exportExcel(params, Users.class, users);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("员工表.xls","UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }





    @PostMapping("/import")
    public RespBean importEmployee(MultipartFile uploadFile){
        ImportParams params = new ImportParams();
        //1.去掉标题行
        params.setTitleRows(1);
        //2.补完员工数据
        List<Nation> nationList = nationService.list();


        try {
            //3.获取上传文件中的员工数据
            List<Users> list = ExcelImportUtil.importExcel(uploadFile.getInputStream(), Users.class, params);
            list.forEach(user -> {
                //设置民族id
                user.setNationId( nationList.get(nationList.indexOf( new Nation(user.getNation().getName()) ) ).getId()  );
            });

            System.out.println(list);
            //4.保存员工数据
            if (userService.saveBatch(list)){
                return RespBean.success("导入成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error("导入失败！");
    }


}














